package com.woniuxy.charin.controller;




import com.woniuxy.charin.model.entity.Permission;
import com.woniuxy.charin.model.entity.Role;
import com.woniuxy.charin.runner.CacheRunner;
import com.woniuxy.charin.service.PermissionService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/permission")
@Slf4j
@Api(tags = "角色管理")
public class PermissionController {
    @Autowired
    private PermissionService permissionService;

    @Autowired
    CacheRunner cacheRunner;
    /**
     * 获取所有权限
     */
    @GetMapping("/getPermissions")
    @ApiOperation("获取所有角色")
    public ResponseEntity<?> list() {
        List<Permission> list = permissionService.getPermissions();
        return ResponseEntity.ok(list);
    }

    /**
     * 根据角色id获取权限id
     * @param roleId
     * @return
     */
    @GetMapping("/getPermissionIdByRole/{roleId}")
    @ApiOperation("根据角色id获取权限id")
    public ResponseEntity<?> getPermissionIdByRole(@PathVariable Integer roleId){
        List<Integer> result = permissionService.getPermissionIdByRole(roleId);
        return ResponseEntity.ok(result);
    }

    /**
     * 设置用户权限
     * @param map
     * @return
     */
    @PostMapping("/assignPermission")
    @ApiOperation("设置用户权限")
    public ResponseEntity<?> assignPermission(@RequestBody Map<String,Object> map){
        Object data = map.get("roleId");
        Long roleId = ((Integer) data).longValue();
        List<Integer> permissionIds = (List<Integer>) map.get("permissionIds");
        permissionService.assignPermission(roleId, permissionIds);
        cacheRunner.init();
        return ResponseEntity.ok(null);
    }






}